﻿using System;
using System.Data.SQLite;
using System.Windows.Forms;

namespace VocabularyApp
{
    public partial class MainForm : Form
    {
        private SQLiteConnection _connection;
        private string _currentEnglishWord;

        public MainForm()
        {
            InitializeComponent();
            InitializeDatabase();
            LoadNextWord();
        }

        private void InitializeDatabase()
        {
            _connection = new SQLiteConnection("Data Source=\"E:\\sqlite\\Vcb.db\";Version=3;");
            _connection.Open();
        }

        private void LoadNextWord()
        {
            string query = "SELECT EnglishWord, ChineseMeaning FROM Words ORDER BY RANDOM() LIMIT 1";
            using (SQLiteCommand cmd = new SQLiteCommand(query, _connection))
            {
                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        _currentEnglishWord = reader["EnglishWord"].ToString();
                        lblChineseMeaning.Text = reader["ChineseMeaning"].ToString();
                    }
                }
            }
        }

        private void txtEnglishWord_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true;
                CheckAnswer();
            }
        }

        private void CheckAnswer()
        {
            if (txtEnglishWord.Text.Trim().Equals(_currentEnglishWord, StringComparison.OrdinalIgnoreCase))
            {
                lblResult.Text = "正确";
            }
            else
            {
                lblResult.Text = "错误";
            }

            // 清空输入框
            txtEnglishWord.Clear();

            // 加载下一个单词
            LoadNextWord();
        }
    }
}